﻿Imports BUS
Imports DTO

Public Class MH_Quanlysach

    'Dat lai mau cua cac control
    Private Sub Reset_Backcolor()
        TH_Tuasach.BackColor = Color.White
        TH_Tacgia.BackColor = Color.White
        TH_Nhxb.BackColor = Color.White
        TH_Namxb.BackColor = Color.White
        TH_Soluong.BackColor = Color.White
        TH_Lydothanhly.BackColor = Color.White
    End Sub

    'Dat lai trang thai va text cac control
    Private Sub Reset_Content()
        TH_Masach.Text = ""
        TH_Tuasach.Text = ""
        TH_Tacgia.Text = ""
        TH_Theloai.SelectedIndex = 0
        TH_Nhxb.Text = ""
        TH_Namxb.Text = ""
        TH_Soluong.Text = ""
        btn_Themsach.Enabled = True
        btn_Capnhatsach.Enabled = False
        btn_Thanhly.Enabled = False
        btn_Thanhlysach.Enabled = False
        TH_Ngaythanhly.Enabled = False
        TH_Lydothanhly.Enabled = False
    End Sub

    'Kiem tra chuoi nhap co phai la so khong
    Private Function Kiemtra_nhapint(ByVal s As String) As String
        Dim Kq As String = ""
        Dim Hop_le As Boolean
        Dim t As Integer

        Hop_le = Integer.TryParse(s, t)
        If Not Hop_le Then
            Kq &= "Cần nhập số" & vbCrLf
        End If

        Return Kq
    End Function

    'Kiem tra thong tin sach co hop le khong
    Private Function Kiemtra_ThongtinSach() As String
        Dim Kq As String = ""
        Dim Hop_le As Boolean
        Dim t As Integer

        Reset_Backcolor()
        'So luong
        Hop_le = Integer.TryParse(TH_Soluong.Text, t)
        Hop_le = Hop_le And t > 0
        If Not Hop_le Then
            TH_Soluong.BackColor = Color.Red
            Kq = "Fail"
        End If
        'Tua sach
        If TH_Tuasach.Text = "" Then
            TH_Tuasach.BackColor = Color.Red
            Kq = "Fail"
        End If
        'Tac gia
        If TH_Tacgia.Text = "" Then
            TH_Tacgia.BackColor = Color.Red
            Kq = "Fail"
        End If
        'Nha xuat ban
        If TH_Nhxb.Text = "" Then
            TH_Nhxb.BackColor = Color.Red
            Kq = "Fail"
        End If

        'Nam xuat ban
        If TH_Namxb.Text = "" Then
            TH_Namxb.BackColor = Color.Red
            Kq = "Fail"
        End If

        Return Kq
    End Function

    'Load
    Private Sub MH_Quanlysach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TH_Chontieuchitimsach.SelectedIndex = 0
        TH_Theloai.SelectedIndex = 0
    End Sub

    'Tim sach
    Private Sub btn_Tracuusach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Tracuusach.Click
        TH_Trangthaitracuusach.Text = "Đang tra cứu ..."
        TH_Trangthaitracuusach.Update()

        Dim output = "OK"

        If TH_Tieuchitimsach.Text = "" Then
            'Lay het bang du lieu
            TH_DS_Sach.DataSource = SachBus.LayBang_Tracuu()
        Else
            'Lay theo tieu chi tim kiem
            Select Case TH_Chontieuchitimsach.SelectedIndex
                Case 0
                    output = Kiemtra_nhapint(TH_Tieuchitimsach.Text)
                    If output = "" Then
                        TH_DS_Sach.DataSource = SachBus.LayBang_Tracuu_Theomasach(TH_Tieuchitimsach.Text)
                    End If
                Case 1
                    TH_DS_Sach.DataSource = SachBus.LayBang_Tracuu_Theotuasach(TH_Tieuchitimsach.Text)
                Case 2
                    TH_DS_Sach.DataSource = SachBus.LayBang_Tracuu_Theotacgia(TH_Tieuchitimsach.Text)
            End Select
        End If

        TH_Trangthaitracuusach.Text = output
    End Sub

    'Lay thong tin cu the cua sach tu danh sach
    Private Sub TH_DS_Sach_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles TH_DS_Sach.CellClick
        Dim ms = TH_DS_Sach.CurrentRow.Cells("Masach").Value
        If ms > 0 Then
            'Init display
            Reset_Backcolor()
            btn_Capnhatsach.Enabled = True
            btn_Thanhly.Enabled = True
            btn_Themsach.Enabled = False

            Dim sach = SachBus.LaySach(ms)
            Dim loai = TheLoaiBus.LayLoai(sach.MTL)
            Dim nganchua = KhoBus.LayNganChua(ms)
            TH_Masach.Text = sach.MS
            TH_Tuasach.Text = sach.Tua_sach
            TH_Tacgia.Text = sach.Tac_gia
            TH_Theloai.Text = loai.Ten_the_loai
            TH_Nhxb.Text = sach.Nha_xuat_ban
            TH_Namxb.Text = sach.Nam_xuat_ban
            TH_Soluong.Text = nganchua.Tong_so
        End If
    End Sub

    'Cap nhat thong tin sach
    Private Sub btn_Capnhatsach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Capnhatsach.Click
        Dim chuoiloi = Kiemtra_ThongtinSach()
        'Cap nhat
        If chuoiloi = "" Then
            Dim sach As New Sach
            Dim nganchua As Kho = KhoBus.LayNganChua(TH_Masach.Text)
            sach.MS = TH_Masach.Text
            sach.Tac_gia = TH_Tacgia.Text
            sach.Tua_sach = TH_Tuasach.Text
            sach.MTL = TH_Theloai.SelectedIndex + 1
            sach.Nha_xuat_ban = TH_Nhxb.Text
            sach.Nam_xuat_ban = TH_Namxb.Text
            nganchua.Tong_so = TH_Soluong.Text
            SachBus.CapNhat(sach)
            KhoBus.CapNhat(nganchua)
            'Reset display
            btn_Tracuusach.PerformClick()
            Reset_Backcolor()
            Reset_Content()
        End If
    End Sub

    'Bat che do thanh ly sach
    Private Sub btn_Thanhly_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Thanhly.Click
        TH_Ngaythanhly.Enabled = True
        TH_Lydothanhly.Enabled = True
        btn_Thanhlysach.Enabled = True
    End Sub

    'Tien hanh thanh ly sach
    Private Sub btn_Thanhlysach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Thanhlysach.Click
        If TH_Lydothanhly.Text = "" Then
            TH_Lydothanhly.BackColor = Color.Red
        Else
            'Thanh ly
            Dim thanhly As New SachThanhLy
            Dim nganchua As Kho = KhoBus.LayNganChua(TH_Masach.Text)
            thanhly.Ly_do = TH_Lydothanhly.Text
            thanhly.Ngay_thanh_ly = TH_Ngaythanhly.Value
            thanhly.MS = TH_Masach.Text
            nganchua.Ton_kho = 0
            nganchua.Tong_so = 0
            SachThanhLyBus.Them(thanhly)
            KhoBus.CapNhat(nganchua)
            'Reset display
            btn_Tracuusach.PerformClick()
            Reset_Backcolor()
            Reset_Content()
        End If
    End Sub

    'Them dau sach moi
    Private Sub btn_Themsach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Themsach.Click
        Dim chuoiloi = Kiemtra_ThongtinSach()
        If chuoiloi = "" Then
            Dim nc As New Kho
            Dim sch As New Sach
            'Them sach
            nc.Tong_so = TH_Soluong.Text
            nc.Ton_kho = TH_Soluong.Text
            sch.MTL = TH_Theloai.SelectedIndex + 1
            sch.Tac_gia = TH_Tacgia.Text
            sch.Tua_sach = TH_Tuasach.Text
            sch.Nha_xuat_ban = TH_Nhxb.Text
            sch.Nam_xuat_ban = TH_Namxb.Text
            SachBus.ThemSach(sch, nc)
            'Reset display
            btn_Tracuusach.PerformClick()
            Reset_Backcolor()
            Reset_Content()
        End If
    End Sub

    'Reset
    Private Sub btn_Reset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Reset.Click
        Reset_Backcolor()
        Reset_Content()
    End Sub

    Private Sub TH_Tieuchitimsach_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TH_Tieuchitimsach.KeyDown
        If e.KeyValue = Keys.Enter Then
            btn_Tracuusach.PerformClick()
        End If
    End Sub
End Class